package com.mooc.ppjoke.utils;

import android.content.ComponentName;

import androidx.fragment.app.FragmentActivity;
import androidx.navigation.ActivityNavigator;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;

import com.mooc.lib_common.MyApplication;
import com.mooc.ppjoke.FixFragmentNavigator;
import com.mooc.ppjoke.data.Destination;

import java.util.HashMap;

/**
 * @ClassName: NavGraphBuilder$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/13 16:27
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class NavGraphBuilder {
    public static void build(NavController controller, FragmentActivity activity,int containId){
        NavigatorProvider provider = controller.getNavigatorProvider();
//        FragmentNavigator navigator = provider.getNavigator(FragmentNavigator.class);
        FixFragmentNavigator navigator = new FixFragmentNavigator(activity, activity.getSupportFragmentManager(), containId);
        provider.addNavigator(navigator);
        ActivityNavigator navigator1 = provider.getNavigator(ActivityNavigator.class);
        HashMap<String, Destination> destConfig = AppConfig.getsDestConfig();
        NavGraph navGraph=new NavGraph(new NavGraphNavigator(provider));
        for (Destination value : destConfig.values()) {
            if(value.getIsFragment() ){
                FragmentNavigator.Destination destination = navigator.createDestination();
                destination.setClassName(value.getClazNam());
                destination.setId(value.getId());
                destination.addDeepLink(value.getPageUrl());
                navGraph.addDestination(destination);
            }else {
                ActivityNavigator.Destination destination = navigator1.createDestination();
                destination.setId(value.getId());
                destination.addDeepLink(value.getPageUrl());
                destination.setComponentName(new ComponentName(MyApplication.getsApplication().getPackageName(),value.getClazNam()));
                navGraph.addDestination(destination);
            }
            if(value.getAsStarter()){
                navGraph.setStartDestination(value.getId());
            }
        }
        controller.setGraph(navGraph);
    }
} 
